package com.example.vp.controller.user;

import com.common.domain.R;
import com.example.vp.domian.enetity.User;
import com.example.vp.service.LoginService;
import com.common.utils.JwtUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping
    public R login(@RequestBody User user){
        System.out.println("user::"+user);
        User loginedUser = loginService.login(user);
        String jwtToken = JwtUtils.getJwtToken(loginedUser.getId());
        Map<Object,Object> map = new HashMap<>();
        map.put("token",jwtToken);
        map.put("name",loginedUser.getUsername());
        map.put("user",loginedUser);
        return  R.ok().data(map);
    }

    /**
     * 获取图形验证码
     * @param response
     * @param uuId
     * @throws IOException
     */
//    @GetMapping("/captcha.jpg/{uuId}")
    public void captcha(HttpServletResponse response, @PathVariable String uuId)
            throws IOException {
        loginService.captcha(uuId,response);
    }

    /**
     * 用户注册
     */
    
}
